/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controladores;

import Beans.Proveedor;
import Beans.Venta;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author CRISTIAN_2
 */
public class Mventa {

    File archivo=new File("Ventas.dat");
    
    private List <Venta> ventas = new LinkedList<Venta>();//lista
    Venta va= new Venta();


public void grabar() throws IOException {

      FileOutputStream   flujoSalida  = new FileOutputStream(archivo);
      ObjectOutputStream objetoSalida = new ObjectOutputStream(flujoSalida);
      objetoSalida.writeObject(this.ventas);
      objetoSalida.close();
     }

public void restaurar() throws IOException,ClassNotFoundException {
        if(archivo.exists()==true){
           FileInputStream flujoEntrada    = new FileInputStream(archivo);
  	   ObjectInputStream objetoEntrada = new ObjectInputStream(flujoEntrada);
  	   List<Venta> vent = (List) objetoEntrada.readObject();
  	   this.setVentas(vent);
           objetoEntrada.close();
        }
 	
    }

   public int GenerarCodigo(){
        int codigo= 0;
        int limite = 10000;
        Random r = new Random();
        codigo = r.nextInt(limite+1);
       return codigo;

    }




    public List<Venta> getVentas() {
        return ventas;
    }
    

    public void setVentas(List<Venta> ventas) {
        this.ventas = ventas;
    }

    public Boolean Existe(int cod){
        boolean sw=false;
        Iterator it = ventas.iterator();
        while(it.hasNext()){
            va = (Venta)it.next();
            if(va.getCodigo()==cod){
                sw=true;
            }
        }
        return sw;
    }


     public void Registrar(Venta ve){
       if(!Existe(ve.getCodigo())){
         getVentas().add(ve);
       }
      }


     public void Eliminar(int cod){
         try{
            Iterator it = ventas.iterator();
            while(it.hasNext()){
            va =(Venta)it.next();
            if(va.getCodigo()== cod){
                ventas.remove(va);
                    try {
                        this.grabar();
                    } catch (IOException ex) {
                        Logger.getLogger(Mventa.class.getName()).log(Level.SEVERE, null, ex);
                    }
                 JOptionPane.showMessageDialog(null,"Eliminado");
             }
         }
         }catch(ConcurrentModificationException e){

         }
        
     }




}
